package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckItemAndCheckGroup;
import com.itheima.mapper.CheckItemDao;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkitemDao;

    @Autowired
    private CheckItemAndCheckGroup checkingItemAndCheckGroup;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        // 开始分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        // 调用DAO层查询
        List<CheckItem> resultList = checkitemDao.findPage(queryPageBean.getQueryString());

        // 构建分页结果
        PageInfo<CheckItem> pageInfo = new PageInfo<>(resultList);
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());

        return pageResult;
    }

    @Override
    public void addCheckItem(CheckItem checkItem) {
        checkitemDao.addCheckItem(checkItem);
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkitemDao.findById(id);
    }



    @Override
    public void editCheckItem(CheckItem checkItem) {
        checkitemDao.editCheckItem(checkItem);
    }

    @Override
    public void deleteById(Integer id) {
        if (checkitemDao.findById(id) == null){
            return;
        }

        checkitemDao.deleteById(id);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkitemDao.findAll();
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkingItemAndCheckGroup.findCheckItemIdsByCheckGroupId(id);
    }
}
